from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep

# chromedriver版本为137版本
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)


def login():
    """
    登录系统
    """
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()


def add_group(group_name, group_description):
    """
    添加一个用户组
    :param group_name: 组名称
    :param group_description: 组描述
    """
    # 点击菜单进入新建组界面
    browser.find_element(By.CSS_SELECTOR, ".menu_titre > a[title='用户和组']").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_user_group_card > a[title='新建组']").click()
    # 输入组名称
    browser.find_element(By.ID, "nom").send_keys(group_name)
    # 输入组描述
    browser.find_element(By.ID, "note").send_keys(group_description)
    # 点击创建组按钮
    browser.find_element(By.NAME, "add").click()


if __name__ == '__main__':
    try:
        login()
        # 定义一个包含组数据的列表
        group_data = [
            {"name": "第一组", "description": "这是第一组呢"},#添加成功
            {"name": "第一组", "description": "这是第一组呢"},#添加失败,名称重复
            {"name": "第三组", "description": "这是第三组呢"},#添加成功
            {"name": "系统管理员组", "description": "拥有最高权限的管理组"},#添加成功
            {"name": "测试工程师组", "description": "负责系统测试的专业团队"},#添加成功
            {"name": "数据分析师组", "description": "专注于数据分析的专家团队"},#添加成功
            {"name": "研发部", "description": "负责产品研发的核心部门"},#添加成功
            {"name": "市场部", "description": "负责市场推广的部门"},#添加成功
            {"name": "财务部", "description": "管理公司财务的部门"},#添加成功
            {"name": "超长组名测试组超长组名测试组超长组名测试组", "description": "测试系统对超长组名的处理能力"},#添加成功
            {"name": "Admin_Group", "description": "Admin group for system management"},#添加成功
            {"name": "研发&测试组", "description": "包含研发和测试人员的混合组"},#添加成功
            {"name": "123456", "description": "纯数字组名测试"},#添加成功
            {"name": "！@#￥%……&*（）", "description": "特殊字符组名测试"},#添加成功
            {"name": "空描述组", "description": ""},#添加成功
            {"name": " ", "description": "空白组名测试"},#添加失败,空名称
            {"name": "Null", "description": "Null值组名测试"},#添加成功
            {"name": "<UNK>", "description": "<UNK>"},#添加失败,空名称
            {"name": "<script>alert('XSS')</script>", "description": "XSS攻击测试组名"},#添加失败，Xss防注入成功
            {"name": "测试结束组", "description": "这是最后一组测试数据"}
        ]

        # 使用for循环动态添加组
        for group in group_data:
            add_group(group["name"], group["description"])
            sleep(2)  # 等待2秒，避免操作过快导致页面响应不及时
    except Exception as e:
        print(e)
    finally:
        sleep(1)
        browser.quit()